<template>
  <div>
    <div class="login-bg">
      <img src="../../assets/loginAdminBg.jpg" />
    </div>
    <el-card class="login-card">
      <span class="admin-title">管理员登录</span>
      <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm">
        <el-form-item prop="email">
          <el-input v-model="loginForm.email" placeholder="输入邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" autocomplete="off" placeholder="输入密码"></el-input>
        </el-form-item>
        <el-form-item style="text-align: center;">
          <el-button style="width:180px" type="primary" @click="login('loginForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from "@/api/admin";
import Cookies from 'js-cookie'

export default {
  data() {
    return {
      loginForm: {
        email: "",
        password: "",
      },
      rules: {
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { type: "email", message: "请输入正确的邮箱地址", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.loginForm).then((res) => {
              //保存token
              Cookies.set('admin',res.data.data.token)
              this.$router.push({ name: "admin" });
            }).catch((err) => {
              this.$message.error(err.response.data.msg);
            });
        } else {
          this.$message.error("请检查输入的数据");
        }
      });
    },
  },
};
</script>

<style scoped>
.login-bg {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  z-index: -1;
}

.login-bg > img {
  width: 100%;
  height: 100%;
  border: 0px;
}

.login-card {
  width: 500px;
  margin: auto;
  margin-top: 10%;
}

.admin-title {
  margin-top: 20px;
  margin-bottom: 35px;
  font-size: 22px;
  line-height: 80px;
}

</style>